package best.driver.android.Activity.Register;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Activity.Order.WorkActivity;
import best.driver.android.Beans.CheckStatus;
import best.driver.android.Beans.DriverDetail;
import best.driver.android.BestApp;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.BestError;
import best.driver.android.manager.UserInfoManager;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class CheckHintActivity extends BaseActivity{

    long mExitTime;
    private String mCheckStatus;

    @InjectView(R.id.btn_goto_submit_check_info)
    Button mBtnGotoSubmitChectInfo;

    @InjectView(R.id.iv_check_icon)
    ImageView mIvCheckIcon;

    @InjectView(R.id.tv_check_hint)
    TextView mTvCheckHint;

    @InjectView(R.id.ll_check_failed_hint)
    LinearLayout mLinearCheckFailedHint;

    @InjectView(R.id.tv_failed_reason)
    TextView mTvFailedReason;

    @OnClick(R.id.btn_goto_submit_check_info)
    protected void gotoSubmitCheckInfo() {
        startActivity(new Intent(this, CheckActivity.class).putExtra("check_status", mCheckStatus));
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_check_info);
        ButterKnife.inject(this, view);
        view.setVisibility(View.GONE);
        setTopTopic("审核");
    }

    private void getDriverDetail() {
        ApiObjectRequest<DriverDetail> request = RequestFactory.createGetDriverDetail();
        request.setListener(new ApiRequest.ApiRequestListener<DriverDetail>() {
            @Override
            public void onRequestSuccess(DriverDetail response) {
                mCheckStatus = response.auditStatus;
                saveUserInfo(response);
                view.setVisibility(View.VISIBLE);
                if (CheckStatus.DRAFT.equals(response.auditStatus)) {
                    showWaitSubmitView();
                } else if (CheckStatus.IN_AUDIT.equals(response.auditStatus)) {
                    showWaitCheckView();
                } else if (CheckStatus.AUDIT_FAILURE.equals(response.auditStatus)) {
                    showCheckFailedView();
                    mTvFailedReason.setText(response.driverAuditInfo.auditRemark);
                } else if (CheckStatus.AUDIT_SUCCESS.equals(response.auditStatus)) {
                    Intent intent = new Intent(CheckHintActivity.this, WorkActivity.class);
                    startActivity(intent);
                    UserInfoManager.getInstance().setCheckStatus(true);
                    UserInfoManager.getInstance().setDriverInfo(response);
                    finish();
                }
            }

            @Override
            public void onRequestError(BestError error) {

            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }

    private void showWaitSubmitView() {
        mBtnGotoSubmitChectInfo.setText("提交审核信息");
        mLinearCheckFailedHint.setVisibility(View.GONE);
        mIvCheckIcon.setImageResource(R.drawable.icon_caution_big);
        mTvCheckHint.setText("请提交车辆信息，通过审核方可接单");
        mTvCheckHint.setTextColor(getColor(R.color.best_red));

    }

    private void showWaitCheckView() {
        mBtnGotoSubmitChectInfo.setText("重新提交审核信息");
        mLinearCheckFailedHint.setVisibility(View.GONE);
        mIvCheckIcon.setImageResource(R.drawable.icon_wait);
        mTvCheckHint.setText("你的车辆信息正在审核,请耐心等待");
        mTvCheckHint.setTextColor(getColor(R.color.best_blue));
    }

    private void showCheckFailedView() {
        mBtnGotoSubmitChectInfo.setText("重新提交审核信息");
        mLinearCheckFailedHint.setVisibility(View.VISIBLE);
        mIvCheckIcon.setImageResource(R.drawable.icon_caution_big);
        mTvCheckHint.setText("你的车辆信息未审核通过,只有通过审核后方可接单");
        mTvCheckHint.setTextColor(getColor(R.color.best_red));
    }

    @Override
    public void onResume() {
        super.onResume();
        getDriverDetail();
    }

    private void saveUserInfo(DriverDetail driverDetail) {
        UserInfoManager manager = UserInfoManager.getInstance();
        manager.setDriverInfo(driverDetail);
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            BestApp.getInstance().exit();
            super.onBackPressed();
        }
    }


}
